#include "adminpage.h"
#include "ui_adminpage.h"
#include <QDebug>
#include <QInputDialog>

AdminPage::AdminPage(std::map<QString, StuData> stuData, std::map<QString, SeatInfo> seatData, QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::AdminPage)
{
    ui->setupUi(this);
    UpdateUserTable(stuData);
    UpdateSeatTable(seatData);
}


AdminPage::~AdminPage()
{
    delete ui;
}

/**
 * @brief AdminPage::UpdateUserTable 更新用户信息表
 * @param stuData
 */
void AdminPage::UpdateUserTable(std::map<QString, StuData> stuData)
{
    QStandardItemModel* model = new QStandardItemModel();
    QStringList labels;
    labels << "学号" << "姓名" << "密码" << "用户类型";
    model->setHorizontalHeaderLabels(labels);
    ui->tableViewUser->setModel(model);
    ui->tableViewUser->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableViewUser->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tableViewUser->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tableViewUser->setSelectionMode(QAbstractItemView::SingleSelection);

    //定义item
    QStandardItem* item;
    int i = 0;
    for(std::map<QString, StuData>::iterator iter = stuData.begin(); iter != stuData.end(); iter++, i++){
        item = new QStandardItem(iter->second.stuid);
        model->setItem(i,0,item);
        item = new QStandardItem(iter->second.name);
        model->setItem(i,1,item);
        item = new QStandardItem(iter->second.passwd);
        model->setItem(i,2,item);
        item = new QStandardItem(iter->second.type);
        model->setItem(i,3,item);
    }
    ui->tableViewUser->setModel(model);
}

/**
 * @brief AdminPage::UpdateSeatTable 更新座位信息表
 * @param seatData
 */
void AdminPage::UpdateSeatTable(std::map<QString, SeatInfo> seatData)
{
    QStandardItemModel* model = new QStandardItemModel();
    QStringList labels;
    labels << "座位ip" << "座位状态" << "用户学号" << "起始时间";
    model->setHorizontalHeaderLabels(labels);
    ui->tableViewSeat->setModel(model);
    ui->tableViewSeat->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableViewSeat->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tableViewSeat->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tableViewSeat->setSelectionMode(QAbstractItemView::SingleSelection);

    //定义item
    QStandardItem* item;
    int i = 0;
    for(std::map<QString, SeatInfo>::iterator iter = seatData.begin(); iter != seatData.end(); iter++, i++){
        QString state;
        if (iter->second.isOnline == false)
            state = "设备离线";
        else state = (iter->second.seatState == SEATSTATE_FREE) ? "可被预约" : "设备正忙";
        item = new QStandardItem(state);
        model->setItem(i,1,item);
        item = new QStandardItem(iter->second.seatAddr);
        model->setItem(i,0,item);
        item = new QStandardItem(iter->second.stuid);
        model->setItem(i,2,item);
        item = new QStandardItem(iter->second.startTime);
        model->setItem(i,3,item);
//        item = new QStandardItem(iter->second.endTime);
//        model->setItem(i,4,item);
    }
    ui->tableViewSeat->setModel(model);
}

/**
 * @brief AdminPage::on_pushButtonDelUser_clicked 删除指定用户
 */
void AdminPage::on_pushButtonDelUser_clicked()
{
    QString stuid = ui->tableViewUser->model()->index(ui->tableViewUser->currentIndex().row(), 0).data().toString();
    if (stuid.isEmpty())
    {
        QMessageBox::warning(NULL, "", "请先选择需要删除的用户", QMessageBox::Yes);
        return;
    }
    qDebug() << "del stuid " << stuid;
    emit PostDelUser(stuid);
}

/**
 * @brief AdminPage::on_pushButtonDelSeat_clicked 删除指定座位
 */
void AdminPage::on_pushButtonDelSeat_clicked()
{
    QString seatid = ui->tableViewSeat->model()->index(ui->tableViewSeat->currentIndex().row(), 0).data().toString();
    if (seatid.isEmpty())
    {
        QMessageBox::warning(NULL, "", "请先选择需要删除的座位", QMessageBox::Yes);
        return;
    }
    qDebug() << "del seatid " << seatid;
    emit PostDelSeat(seatid);
}

/**
 * @brief AdminPage::on_pushButtonEditPasswd_clicked 修改某个用户的密码
 */
void AdminPage::on_pushButtonEditPasswd_clicked()
{
    StuData stuData;
    stuData.stuid = ui->tableViewUser->model()->index(ui->tableViewUser->currentIndex().row(), 0).data().toString();
    if (stuData.stuid.isEmpty())
    {
        QMessageBox::warning(NULL, "", "请先选择需要修改的用户", QMessageBox::Yes);
        return;
    }

    bool res = false;
    stuData.passwd = QInputDialog::getText(this, "", "请输入新密码", QLineEdit::Password, "", &res);
    if (res && !stuData.passwd.isEmpty())
    {
        emit PostEditUserPassword(stuData);
    }
}

/**
 * @brief AdminPage::on_pushButtonUnbind_clicked 解除指定用户与座位的预约/使用关系
 */
void AdminPage::on_pushButtonUnbind_clicked()
{
    QString stuid = ui->tableViewSeat->model()->index(ui->tableViewSeat->currentIndex().row(), 2).data().toString();
    if (stuid.isEmpty())
    {
        QMessageBox::warning(NULL, "", "请先选择需要解除预约关系的座位", QMessageBox::Yes);
        return;
    }
    qDebug() << "unbind stuid " << stuid;
    emit PostDelSeat(stuid);
}
